<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { getCoinServicePage } from '../../api/coinService'
import { HttpStatusCode } from "axios"
import { ElMessage } from "element-plus"
import CoinServiceTable from './components/coinServiceTable/index.vue'
import AddCoinServiceForm from './components/addCoinServiceForm/index.vue'
import UpdateCoinServiceForm from './components/updateCoinServiceForm/index.vue'

const showAddBox = ref(false)
const showEditBox = ref(false)
let coinServiceList = ref<ListCoinService[]>([])
const remark = ref('')
let updateService = ref<ListCoinService>()
const pagination = reactive<Page>({})
let loading = ref(false)
//子组件方法
const tableRef = ref()

onMounted(() => {
  getData(1, remark.value)
})
const changeShowAddBox = () => {
  showAddBox.value = !showAddBox.value
}
const changeShowEditBox = (formData: ListCoinService) => {
  showEditBox.value = !showEditBox.value
  updateService.value = formData
}
const cancelEditBox = () => {
  showEditBox.value = !showEditBox.value
}
const compare = (coinA: ListCoinService, coinB: ListCoinService): number => {
  return coinA.coin - coinB.coin
}
const getData = async (page: number, remark: string) => {
  loading.value = true
  const res = await getCoinServicePage(page, remark)
  if(res.statusCode === HttpStatusCode.Ok) {
    coinServiceList.value = res.data.data as unknown as ListCoinService[]
    coinServiceList.value.sort(compare)
    pagination.current = page
    pagination.total = res.data.total
    pagination.pageSize = res.data.pageRow
    loading.value = false
  } else {
    ElMessage({
      message: '获取失败，请重试',
      type: 'error',
    })
  }
}
const searchCoin = () => {
  getData(1, remark.value)
}
const handleChange = (val: number) => {
  getData(val, remark.value);
}
const manyDel = () => {
  tableRef.value.delMany()
}
</script>

<template>
  <div class="container">
    <div class="header">
      <div class="search">
        <el-input class="searchInp" clearable v-model="remark" placeholder="输入备注">
        </el-input>
        <el-button class="searchBtn" type="primary" @click="searchCoin">搜索</el-button>
      </div>
      <div class="btnGroup">
        <el-button class="addBtn" type="success" @click="changeShowAddBox">添加服务</el-button>
        <el-button class="addBtn" type="danger" @click="manyDel">删除服务</el-button>
      </div>
    </div>
    <div class="table">
      <CoinServiceTable ref="tableRef" :coin-service-list="coinServiceList" :loading="loading" @increase="getData" @updateData="changeShowEditBox" />
      <el-pagination
          class="pag"
          background
          layout="prev, pager, next"
          :total="pagination.total"
          :current-page="pagination.current"
          @current-change="handleChange"
      />
    </div>
    <el-drawer
        v-model="showAddBox"
        title="添加个人车源"
        direction="rtl"
    >
      <!--    添加表单-->
      <AddCoinServiceForm @increase="getData" @closeShow="changeShowAddBox"  />
    </el-drawer>
    <el-drawer
        v-model="showEditBox"
        title="修改个人车源"
        direction="rtl"
    >
      <!--    修改表单 UpdatePersonalChannelForm-->
      <UpdateCoinServiceForm @increase="getData" @closeShow="cancelEditBox"  :coin-service-form="updateService"/>
    </el-drawer>
  </div>
</template>

<style lang="less" scoped>
.container {
  width: 100%;
  height: 100%;
  .header {
    width: 95%;
    height: 60px;
    margin: 0 auto;
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .search {
      margin-left: 20px;
      display: flex;
      align-items: center;
      .searchInp {
        width: 300px;
      }
      .searchBtn {
        margin-left: 10px;
      }
    }
    .btnGroup {
      margin-right: 20px;
      display: flex;
      align-items: center;
    }
  }
  .table {
    width: 95%;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    .pag {
      margin-top: 20px;
    }
  }
}
</style>